<template>
	<view class="main-page">
		<view class="search-box">
			<view class="input-box">
				<view class="row-container width-100p">
					<image class="wh-30 mr-20" src="../../static/common/ic_search.png" 
					style="flex-shrink: 0;"></image>
					<input 
						v-model="keyword"
						class="font-333-28 width-100p"
						placeholder-class="font-999-28"
						placeholder="请输入客户名称或电话"
						@input="handleSearchClient"
					/>
				</view>
				<image class="wh-34 ml-20" src="../../static/common/ic_clear.png" v-show="keyword"
				 @click="clearInput" style="flex-shrink: 0;"></image>
			</view>
		</view>
		
		<view class="column-container width-100p mt-133 bg-white pb-20" >
			<view class="list-item" v-for="(item ,index) in clientList" :key="index"
			@click="handleChosenClient(item)">
				<view class="row-container width-100p">
					<image class="avatar" src="../../static/common/ic_avatar_female.png" v-if="item.sex == 0"></image>
					<image class="avatar" src="../../static/common/ic_avatar_male.png" v-if="item.sex == 1"></image>
					<view class="column-container mt-4">
						<text class="font-333-32 font-weight-600">{{item.realName}}</text>
						<view class="row-container mt-4">
							<text class="font-333-26">ID：{{item.id}}</text>
							<view class="column-line"></view>
							<text class="font-333-26" v-if="item.sex == 1">男</text>
							<text class="font-333-26" v-if="item.sex == 0">女</text> 
							<view class="column-line"></view>
							<text class="font-333-26">联系电话：{{item.mobile}}</text>
						</view>
					</view>
				</view>
				<view class="source-box">
					<text class="font-999-22" v-if="item.channelMsg">来源：{{item.channelMsg}}</text>
					<view class="column-line" v-if="item.channelMsg"></view>
					<text class="font-999-22" v-if="item.brithdayTimeStr">生日：{{item.brithdayTimeStr}}</text>
					<view class="column-line" v-if="item.brithdayTimeStr"></view>
					<text class="font-999-22" v-if="item.lastShopTime">上次到店：{{item.lastShopTime}}</text>
					<text class="font-999-22" v-else>暂无到店记录</text>
				</view>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				clientList:[],
				page:1,
				hasMoreData:false,
				keyword:''
			}	
		},
		
		onLoad() {
			this.loadDataList()
		},	
		
		onPullDownRefresh() {
			this.page = 1
			this.loadDataList()
		},
		
		onReachBottom() {
			if (this.hasMoreData){
				this.page ++
				this.loadDataList()
			}
		},
		
		methods: {
			handleSearchClient(e){
				this.keyword = e.detail.value
				this.page = 1
				this.loadDataList()
			},
			
			clearInput(){
				this.keyword = ""
				this.page = 1
				this.loadDataList()
			},
			
			loadDataList(){
				let that = this
				this.http({
					url:'/user/custom/findCustomList',
					method:'POST',
					data:{
						pageNo: that.page,
						pageSize: 20,
						keyWord:this.keyword
					},
					success(res){
						if (res.code == 200){
							if (that.page == 1){
								that.clientList = res.data
							} else{
								that.clientList = [...that.clientList, ...res.data]
							}
							that.hasMoreData = res.data.length >= 20
						}
						uni.stopPullDownRefresh()
					},
					failed(e){
						uni.stopPullDownRefresh()
					}
				})
			},
			
			handleChosenClient(item){
				uni.$emit('chosenClient' , item)
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.search-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 113rpx;
		background: white;
		position: fixed;
		z-index: 100;
		
		.input-box{
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			background: #F5F5F5;
			border-radius: 10rpx;
			width: calc(100% - 100rpx);
			height: 70rpx;
			margin: 0rpx 30rpx;
			padding: 0rpx 20rpx;
			
		}
	}
	
	.list-item{
		display: flex;
		flex-direction: column;
		width: calc(100% - 60rpx);
		margin: 30rpx 30rpx 20rpx 30rpx;
		align-items: center;
		position: relative;
		
		.divider-line{
			width: 100%;
			height: 1rpx;
			background: #E8E8E8;
			margin-top: 24rpx;
		}
		
		.avatar{
			width: 80rpx;
			height: 80rpx;
			margin-right: 24rpx;
		}
		
		.column-line{
			width: 1rpx;
			height: 16rpx;
			background: #999999;
			margin: 0rpx 16rpx;
		}
		
		.source-box{
			background: #F7F7F7;
			border-radius: 4rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			width: calc(100% - 28rpx);
			padding: 6rpx 14rpx;
			margin-top: 8rpx;
		}
	}
	
</style>
